package queue;

public class LinkedQueue<T> implements QueueADT<T>{
    private Node<T> head;
    private Node<T> tail;

    private int size;
    public LinkedQueue() {
        this.head=new Node<>();
        this.tail=new Node<>();
        size=0;
    }

    /**
     * 链表是无界的，不涉及队满，往头插
     * @param t
     * @return
     */
    @Override
    public boolean enqueue(T t) {
        Node<T> temp = new Node<>(t);
        if (head.next==null){
            head.next=temp;
        }else {
            tail.next=temp;

        }
        tail=temp;
        size++;

        return true;
    }

    @Override
    public T dequeue() {
        if (head.next==null){
            System.out.println("对空");
            return null;
        }
        T res=head.next.data;
        head.next=head.next.next;
        size--;
        return res;
    }

    @Override
    public T first() {
        return head.next.data;
    }

    @Override
    public T last() {
        return tail.data;
    }

    @Override
    public void show() {
        Node<T> temp = head.next;
        while (temp!=null){
            System.out.println(temp.data);
            temp=temp.next;
        }
    }

    public static void main(String[] args) {
        LinkedQueue<Integer> queue = new LinkedQueue<>();
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        queue.enqueue(4);
        System.out.println(queue.first());
        System.out.println(queue.last());
    }
}
